#!/usr/bin/env python
try:
    import json
except:
    from helper import HelpJson as json
import sys
import class_set
import helper


class FollowListMapper(helper.MapperHelper):

    def loader(self, line):
        line = line.strip()
        node_operator = json.loads(line)
        if node_operator["type"] != "delfans":
            for node in node_operator["staruids"]:
                left = node_operator["uid"]
                right = class_set.UserNode(node, node_operator["addtime"], node_operator["type"])
                yield (left, right)
        else:
            for node in node_operator["staruids"]:
                left = node
                right = class_set.UserNode(node_operator["uid"], node_operator["addtime"], "delstars")
                yield(left, right)

    def mapper(self):
        """
        Mapper for get follow list.
        When operator is "delstars" and "addattention", just prompt the object to stdout.
        But when operator is "delfans", pairs of user IDs should be inverted and type of operator
        should be fixed as "delstars" from "delfans".
        Note that JSON is used as presistent storage.

        Argument:
            None
        Return:
            None
        Stdin:
            The original datasets of user action.
        Stdout:
            'userID\tUserNode'
        """
        for line in sys.stdin:
            for left, right in self.loader(line):
                self.allocator(left, right)

def main():
    fm = FollowListMapper()
    fm.mapper()
    
    

if __name__ == '__main__':
    main()
